package cn.jn.lhm.service.config.exception.handler;

import cn.hutool.core.date.DateUtil;
import cn.jn.lhm.service.config.exception.GlobalException;
import cn.jn.lhm.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author LHM
 * @Tips 热爱每一天，奋斗每一天
 * @date 2020/5/28 - 6:29
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(Throwable.class)
    @ResponseBody
    public R error(Exception e){
        log.error(e.getMessage() + "错误时间为：" + DateUtil.now());
        e.printStackTrace();
        return R.error().message("系统异常，请稍后再试");
    }

    @ExceptionHandler(NullPointerException.class)
    @ResponseBody
    public R errorForNullException(NullPointerException e){
        log.error(e.getMessage() + "错误时间为：" + DateUtil.now());
        e.printStackTrace();
        return R.error().message("系统出现了空异常");
    }


    @ExceptionHandler(GlobalException.class)
    @ResponseBody
    public R errorForDIY(GlobalException e){
        log.error(e.getMessage() + "错误时间为：" + DateUtil.now());
        e.printStackTrace();
        return R.error().message(e.getMsg()).code(e.getCode());
    }
}
